<template>
  <el-container>
    <el-aside width="220px" v-loading="showGrouploading">
      <el-container>
        <el-header>
          <el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
        </el-header>
        <el-main class="nopadding">
          <el-tree ref="group" class="menu" node-key="id" :data="group" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick">
          </el-tree>
        </el-main>
      </el-container>
    </el-aside>
    <el-container>
      <el-header class="searchBox">
        <el-form :inline="true" class="form-inline">
          <el-form-item>
            <!-- label="模糊查询" -->
            <el-input v-model="params.keywords" placeholder="用户信息" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button v-auth="1020501" type="primary" icon="el-icon-search" @click="handleSearch()">查询</el-button>
            <el-button v-auth="1020502" type="primary" icon="el-icon-plus" @click="handleAdd()">新增</el-button>
            <!-- <el-button v-auth="1020503" type="primary" plain :disabled="selection.length==0">分配角色</el-button>
            <el-button v-auth="1020504" type="primary" plain :disabled="selection.length==0">密码重置</el-button> -->
          </el-form-item>
        </el-form>
      </el-header>
      <el-main class="nopadding">
        <scTable ref="table" :apiObj="listData" v-bind:params="params" row-key="id" @selection-change="selectionChange" stripe border remoteSort>
          <el-table-column type="selection" width="50"></el-table-column>
          <el-table-column label="登录账号" prop="userName" width="150" sortable></el-table-column>
          <el-table-column label="真实姓名" prop="realName" width="150" sortable></el-table-column>
          <el-table-column label="手机号码" prop="telphone" width="120"></el-table-column>
          <el-table-column label="性别" prop="gender" width="70"></el-table-column>
          <el-table-column label="排序" prop="sort" width="80" sortable></el-table-column>
          <el-table-column label="状态" prop="isValid" width="100">
            <template #default="scope">
              <el-switch v-model="scope.row.isValid" @change="changeSwitch(scope.row)" :loading="scope.row.$switchStatus" :active-value="true" :inactive-value="false"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="角色" prop="roleNames" width="200" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="部门" prop="departmentName" width="200" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="岗位" prop="postNames" width="200" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="操作" align="center" width="420" fixed="right">
            <template #default="scope">
              <el-button-group>
                <el-button v-auth="1020503" text type="primary" size="small" icon="el-icon-menu" @click="handleResetPsw(scope.row, scope.$index)">密码重置</el-button>
                <el-button v-auth="1020504" text type="primary" size="small" icon="el-icon-search" @click="handleShow(scope.row, scope.$index)">查看</el-button>
                <el-button v-auth="1020505" text type="primary" size="small" icon="el-icon-edit" class="btnBlue1" @click="handleEdit(scope.row, scope.$index)">编辑</el-button>
                <el-popconfirm title="确定删除吗？" @confirm="handleRemove(scope.row, scope.$index)">
                  <template #reference>
                    <el-button v-auth="1020506" type="text" size="small" icon="el-icon-remove" class="btnRed">删除</el-button>
                  </template>
                </el-popconfirm>
              </el-button-group>
            </template>
          </el-table-column>
        </scTable>
      </el-main>
    </el-container>
  </el-container>

  <!-- 编辑等操作弹窗 -->
  <save-dialog v-if="dialog.save" ref="saveDialog" @success="callBack" @closed="dialog.save = false">
  </save-dialog>
</template>
<script>
//引用对象
import saveDialog from "./components/save";
export default {
  name: 'user',
  components: {
    saveDialog
  },
  data() {
    return {
      showGrouploading: false,
      groupFilterText: '',
      group: [],
      groupsProps: {
        value: 'id',
        label: 'name',
        children: 'children',
        emitPath: false,
        checkStrictly: true
      },
      params: {
        keywords: "",
        departmentId: null,
      },
      listData: this.$API.user.getPageList,
      selection: [],
      dialog: {
        save: false
      },
    }
  },
  watch: {
    groupFilterText(val) {
      this.$refs.group.filter(val);
    }
  },
  mounted() {
    this.getGroup()
  },
  methods: {
    //获取数据
    handleSearch() {
      this.$refs.table.loadData(this.params);
    },
    //查看
    handleShow(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("show").setData(row);
      });
    },
    //添加
    handleAdd() {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open();
      });
    },
    //编辑
    handleEdit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    //表格内开关
    async changeSwitch(row) {
      row.$switchStatus = true;
      var par = {
        id: row.id
      };
      var res = await this.$API.user.forbidOrEnable.put(par);
      if (res.code != 200) {
        this.$Message.warning(res.msg);
      }
      delete row.$switchStatus;
    },
    //单条删除
    async handleRemove(row, index) {
      var reqData = { id: row.id }
      var res = await this.$API.user.deleteById.delete(reqData);
      if (res.code == 200) {
        //这里选择刷新整个表格 OR 插入/编辑现有表格数据
        this.$refs.table.tableData.splice(index, 1);
        this.$Message.success("删除成功");
      } else {
        this.$Message.warning(res.msg);
      }
    },
    //密码重置
    async handleResetPsw(row, index) {
      var reqData = { id: row.id }
      var res = await this.$API.user.resetPassword.put(reqData);
      if (res.code == 200) {
        this.$Message.success(res.msg);
      } else {
        this.$Message.warning(res.msg);
      }
    },
    //表格选择后回调事件
    selectionChange(selection) {
      this.selection = selection;
    },
    //加载树数据
    async getGroup() {
      this.showGrouploading = true;
      var res = await this.$API.department.getTreeList.get();
      this.showGrouploading = false;
      this.group = res.data;
    },
    //树过滤
    groupFilterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    //树点击事件
    groupClick(data) {
      var params = {
        departmentId: data.id
      }
      this.$refs.table.reloadData(params)
    },
    //本地更新数据[回调]
    callBack(data, mode) {
      if (mode == 'add') {
        this.$refs.table.reloadData();
      } else if (mode == 'edit') {
        this.$refs.table.tableData.filter(item => item.id === data.id).forEach(item => {
          Object.assign(item, data);
        });
      }
    },
  }
}
</script>

<style>
</style>
